package clientservice;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 *
 * @author ZeM
 */
public class keyreader {

    /**
     * Read key file
     * @param name the name of key file
     * @return true if read successfully
     */
    public byte[] read(String name) {

        try {
            FileInputStream fis = new FileInputStream("key" + File.separatorChar + name + ".key");
            DataInputStream dis = new DataInputStream(fis);
            byte[] readed = new byte[129];
            try {
                int readedl = 0;

                do {
                    byte[] buffer = new byte[129 - readedl];
                    int length=dis.read(buffer, readedl, 129 - readedl);
                    for(int k=readedl;k<readedl+length;k++){
                    readed[k]=buffer[k-readedl];
                    }
                    readedl+=length;
                } while (readedl < 129);
                dis.read(readed);

                dis.close();
                fis.close();
                return readed;
            } catch (IOException ex) {
                return null;
            }
        } catch (FileNotFoundException ex) {
            return null;
        }

    }
}
